package interviewClassic150;

public class CanConstruct {
    public boolean canConstruct(String ransomNote, String magazine) {
        //定义一个哈希表来记录ransomNote中字母的出现次数
        int[] map = new int[26];
        for (int i = 0; i < ransomNote.length(); i++) {
            char curChar = ransomNote.charAt(i);
            int index = curChar - 'a';
            map[index]++;
        }
        //然后再遍历magazine来删减字符
        for (int i = 0; i < magazine.length(); i++) {
            char curChar = magazine.charAt(i);
            int index = curChar - 'a';
            if (map[index] > 0) map[index]--;
        }
        //最后遍历map，如果有非0，返回false
        for (int i = 0; i < 26; i++) {
            if (map[i] > 0) return false;
        }

        return true;
    }
}
